import openpyxl
import re
import random

# 打开 Excel 文件
file_path = r"D:\Windows\工作簿11.xlsx"   # 改成你的文件路径
workbook = openpyxl.load_workbook(file_path)
sheet = workbook.active  # 默认第一个sheet

# 序号映射
order_map = {
    "一": "第一", "二": "第二", "三": "第三", "四": "第四", "五": "第五",
    "六": "第六", "七": "第七", "八": "第八", "九": "第九", "十": "第十"
}

# 方向词
directions = ["东", "西", "南", "北", "前", "后", "东南", "东北", "西南", "西北"]

# 山岭类结尾
mountain_suffix = ["山", "岭", "峰"]

# 途经/通往 随机概率 (80%途经, 20%通往)
def random_pass_or_to():
    return "途经" if random.random() < 0.8 else "通往"

# 遍历行
for row in range(2, sheet.max_row + 1):
    area_name = sheet[f"A{row}"].value  # A列（村/社区）
    road_name = sheet[f"B{row}"].value  # B列（道路名）
    if road_name is None:
        continue

    text = ""
    base_name = road_name
    suffix = ""

    # 道路后缀（路/街/巷/道）
    if road_name.endswith(("路", "街", "巷", "道")):
        suffix = road_name[-1]
        base_name = road_name[:-1]

    # 去掉 A列中“村/社区”等后缀，得到核心名字
    area_core = None
    if area_name:
        area_core = area_name.replace("村", "").replace("社区", "")

    # 如果 B列包含 A列名字，剔除掉
    if area_core and area_core in base_name:
        resident_name = base_name.replace(area_core, "", 1)  # 只替换掉一次，避免误删
    else:
        resident_name = base_name

    # 1. 判断是否带序号
    match = re.match(r"(.+?)([一二三四五六七八九十]+)$", resident_name)
    if match:
        name, order = match.groups()
        order_text = order_map.get(order, f"第{order}条")
        if area_name and area_core in base_name:
            text = f"因该{suffix}位于{area_name}，且{random_pass_or_to()}{name}居民点，按序数排列{order_text}，故名。"
        else:
            text = f"因该{suffix}{random_pass_or_to()}{name}居民点，按序数排列{order_text}，故名。"

    # 2. 判断是否方向
    elif any(resident_name.endswith(d) for d in directions):
        for d in directions:
            if resident_name.endswith(d):
                name = resident_name[:-len(d)]
                if area_name and area_core in base_name:
                    text = f"因该{suffix}位于{area_name}，且{random_pass_or_to()}{name}居民点{d}侧，故名。"
                else:
                    text = f"因该{suffix}{random_pass_or_to()}{name}居民点{d}侧，故名。"
                break

    # 3. 判断是否中路
    elif resident_name.endswith("中"):
        name = resident_name[:-1]
        if area_name and area_core in base_name:
            text = f"因该{suffix}位于{area_name}，且位于{name}居民点中部，故名。"
        else:
            text = f"因该{suffix}位于{name}居民点中部，故名。"

    # 4. 判断是否山岭类
    elif any(resident_name.endswith(m) for m in mountain_suffix):
        name = resident_name
        if area_name and area_core in base_name:
            text = f"因该{suffix}位于{area_name}，且通往{name}，故名。"
        else:
            text = f"因该{suffix}通往{name}，故名。"

    # 5. 普通情况
    else:
        if area_name and area_core in base_name:
            text = f"因该{suffix}位于{area_name}，且{random_pass_or_to()}{resident_name}居民点，故名。"
        else:
            text = f"因该{suffix}{random_pass_or_to()}{resident_name}居民点，故名。"

    # 写入C列
    sheet[f"C{row}"] = text

# 保存文件
output_path = r"D:\Windows\道路_生成文案.xlsx"
workbook.save(output_path)
print("生成完成！结果已保存到", output_path)
